/**
 * @author: Niimp
 * @date: 2023/11/23 10:35
 * @statement:
 */
package com.dmgis.hndz.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.dmgis.hndz.service.RiskManagementService;
import com.dmgis.hndz.api.ZhcxApi;
import com.dmgis.hndz.common.utils.StringUtil;
import com.dmgis.hndz.mapper.RiskManagementMapper;
import com.dmgis.hndz.pojo.*;
import com.dmgis.hndz.pojoVo.HiddenDangerVo;
import com.dmgis.hndz.pojoVo.RiskZoneVo;
import com.dmgis.hndz.pojoVo.Zhaa11aVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;

@Service
@DS("slave_qxdata")
public class RiskManagementServiceImpl implements RiskManagementService {
    @Resource
    RiskManagementMapper riskManagementMapper;
    private ZhcxApi zhcxApi = new ZhcxApi();


    public Map<String, Object> queryAllXzqh() throws Exception {
        try {
            Map<String, Object> map = new HashMap<>();
            List<ZDDA01A> list = this.riskManagementMapper.queryAllXzqh();
            Map<String, String> hnMap = new HashMap<>();
            List<String[]> cityArr = (List) new ArrayList<>();
            Map<String, Object> strcountyArr = new HashMap<>();
            for (ZDDA01A zdda01a : list) {
                hnMap.put(zdda01a.getZdda01a010(), zdda01a.getZdda01a020());
                if (zdda01a.getZdda01a010().endsWith("00") &&
                        !"430000".equals(zdda01a.getZdda01a010()) && zdda01a
                        .getZdda01a010().length() == 6) {
                    cityArr.add(new String[]{zdda01a.getZdda01a010(), zdda01a.getZdda01a020()});
                }
                if (!zdda01a.getZdda01a010().endsWith("00") && zdda01a
                        .getZdda01a010().length() == 6) {
                    String shi = zdda01a.getZdda01a010().substring(0, 4) + "00";
                    if (!strcountyArr.containsKey(shi)) {
                        strcountyArr.put(shi, new ArrayList());
                    }
                    List<String[]> xians = (List<String[]>) strcountyArr.get(shi);
                    xians.add(new String[]{zdda01a.getZdda01a010(), zdda01a.getZdda01a020()});
                }
            }
            map.put("hnMap", hnMap);
            map.put("cityArr", cityArr);
            map.put("strcountyArr", strcountyArr);
            return map;
        } catch (Exception ex) {
            ex.getStackTrace();
            throw new Exception("方法名：RiskManagementServiceImpl.queryAllXzqh," + ex.getMessage());
        }
    }

    /**
     * @Description: 综合查询-行政单元-调查点查询
     */
    @Override
    public List<?> queryZhdBysy(City city) {
        //灾害点
        if ("sbd".equals(city.getCxtypes())) {
            return zhcxApi.selectSbdNumBySy(city);
        }
        return zhcxApi.queryZhcxXzdyByDcd(city);
    }


    /**
     * @Description: 综合查询-灾害点详情
     */
    @Override
    public List<?> queryZhcxZhd(City city, HttpSession session) {
        List<ZhcxZhd> list = null;
        if (city.getYears() == null) {
            Calendar cale = null;
            cale = Calendar.getInstance();
            int year = cale.get(Calendar.YEAR);
            city.setYears(year + "");
        }
        if ("zq".equals(city.getCxtypes())) {
            list = zhcxApi.queryZhcxZq(city);    //综合查询-灾害点灾情
        } else if ("xq".equals(city.getCxtypes())) {
            list = zhcxApi.queryZhcxXq(city);   //@Description:综合查询-灾害点灾情
        } else if ("sbd".equals(city.getCxtypes())) {
            //查询速报点
            list = zhcxApi.selectSbdInfo(city);  //@Description:速报点信息
        } else if ("hxd".equals(city.getCxtypes())) {
            list = zhcxApi.selectCancelZhd(city);
        } else {
            //地图初始化查询点
            if (session!=null) {
                List<ZhcxZhd> noKeyAllList = (List<ZhcxZhd>) session.getAttribute("noKeyAllList");
                if (city.getNoKeyPoint() != null
                        && !"".equals(city.getNoKeyPoint())
                        && noKeyAllList != null
                        && noKeyAllList.size() > 0) {
                    return noKeyAllList;
                }
            }
                list = zhcxApi.queryZhcxZhd(city);   //综合查询-灾害点详情
                if (city.getNoKeyPoint() != null
                        && !"".equals(city.getNoKeyPoint())) {
                    session.setAttribute("noKeyAllList", list);
                }

        }
        return list;
    }

    @Override
    public HiddenDangerVo getHiddenDangerPointInfo(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        Integer zhaa01c020 = null;
        String zhaa11a030 = jsonObject.getString("zhaa11a030");//市
        String zhaa11a032 = jsonObject.getString("zhaa11a032");//县
        String zhaa11a024 = jsonObject.getString("zhaa11a024");//灾害类型
        String zhaa11a012 = jsonObject.getString("zhaa11a012");//统一编号
        String zhaa11a022 = jsonObject.getString("zhaa11a022");//灾害名称关键字
        String year = jsonObject.getString("zhaa01c020");//隐患点年份

        if (!StringUtil.IsNullOrEmpty(year))
            zhaa01c020 = Integer.parseInt(year);

        List<Zhaa11aVO> hiddenDangerPointList =
                riskManagementMapper.getHiddenDangerPointInfo(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);
        List<Zhaa11aVO> hiddenCodeImageList =
                riskManagementMapper.getHiddenCodeImageListo(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);

//        List<Zhaa11aVO> hiddenDangerPointList =
//                riskManagementMapper.getHiddenDangerPointInfo1(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);
//        List<Zhaa11aVO> hiddenCodeImageList =
//                riskManagementMapper.getHiddenCodeImageListo1(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);

        Map<String, Integer> countMap = new HashMap<>();
        Map<String, Integer> distributionMap = new HashMap<>();
        // 统计各个灾害类型的数量
        countMap = hiddenDangerPointList.stream()
                .collect(Collectors.groupingBy(Zhaa11aVO::getZhaa11A024, Collectors.summingInt(e -> 1)));

        // 计算总的灾害类型数量
        int totalCount = countMap.values().stream().mapToInt(Integer::intValue).sum();

        if (StringUtil.IsNullOrEmpty(zhaa11a032)){
            // 按县统计隐患点的数量
            distributionMap = hiddenDangerPointList.stream()
                    .collect(Collectors.groupingBy(Zhaa11aVO::getZhaa11A032,Collectors.summingInt(e -> 1)));
        }else {
            // 按乡统计隐患点的数量
            distributionMap = hiddenDangerPointList.stream().map(i->{
                if (StringUtil.IsNullOrEmpty(i.getZhaa11A034())){
                    i.setZhaa11A034("其他");
                }
                return i;
            }).collect(Collectors.groupingBy(Zhaa11aVO::getZhaa11A034,Collectors.summingInt(e -> 1)));
        }


        // 将总计数量存入Map
        countMap.put("总计", totalCount);
        System.out.println("===================================================================================");
//        for (Zhaa11aVO zhaa11aVO : hiddenDangerPointList) {
//            System.out.println(zhaa11aVO);
//        }
//        for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
//            String disasterType = entry.getKey();
//            int count = entry.getValue();
//            System.out.println(disasterType + ":" + count);
//        }



        HiddenDangerVo hiddenDangerVo = new HiddenDangerVo(hiddenDangerPointList,hiddenCodeImageList, countMap,distributionMap);
        return hiddenDangerVo;
    }

    @Override
    public RiskZoneVo queryRiskZoneInfo(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }


        String zhfx01a030 = jsonObject.getString("city");//市
        String zhfx01a040 = jsonObject.getString("county");//县
        String zhfx01a090 = jsonObject.getString("riskLevel");//风险等级
        String zhfx01a015 = jsonObject.getString("keyword");//风险区名称关键字

        List<Zhfx01a> zhfx01aList = riskManagementMapper.queryRiskZoneInfo(zhfx01a030, zhfx01a040, zhfx01a090, zhfx01a015);


        Map<String, Integer> levelMap = new HashMap<>();
        Map<String, Integer> distributionMap = new HashMap<>();

        // 统计各个风险等级的数量
        levelMap = zhfx01aList.stream()
                .collect(Collectors.groupingBy(Zhfx01a::getZhfx01a090,Collectors.summingInt(e -> 1)));
//        System.out.println("===================================================================================");
//        for (Map.Entry<String, Integer> entry : levelMap.entrySet()) {
//            String disasterType = entry.getKey();
//            int count = entry.getValue();
//            System.out.println(disasterType + ":" + count);
//        }

//        if (StringUtil.IsNullOrEmpty(zhfx01a040)){
//            // 按县统计风险区的数量
//            distributionMap = zhfx01aList.stream()
//                    .collect(Collectors.groupingBy(Zhfx01a::getZhfx01a030,Collectors.summingInt(e -> 1)));
//        }else {
//            // 按乡统计风险区的数量
//            distributionMap = zhfx01aList.stream()
//                    .collect(Collectors.groupingBy(Zhfx01a::getZhfx01a040,Collectors.summingInt(e -> 1)));
//        }

        //若没有传入县编码
        if (StringUtil.IsNullOrEmpty(zhfx01a040)){
            // 按县统计风险区的数量
            distributionMap = zhfx01aList.stream()
                    .collect(Collectors.groupingBy(Zhfx01a::getZhfx01a040,Collectors.summingInt(e -> 1)));
        }else {
            // 按乡统计风险区的数量
            distributionMap = zhfx01aList.stream()
                    .collect(Collectors.groupingBy(Zhfx01a::getZhfx01a050,Collectors.summingInt(e -> 1)));
        }

        RiskZoneVo riskZoneVo = new RiskZoneVo(zhfx01aList,levelMap,distributionMap);
        return riskZoneVo;
    }

    @Override
    public HiddenDangerVo getHiddenDangerPoint(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        Integer zhaa01c020 = null;
        String zhaa11a030 = jsonObject.getString("zhaa11a030");//市
        String zhaa11a032 = jsonObject.getString("zhaa11a032");//县
        String zhaa11a024 = jsonObject.getString("zhaa11a024");//灾害类型
        String zhaa11a012 = jsonObject.getString("zhaa11a012");//统一编号
        String zhaa11a022 = jsonObject.getString("zhaa11a022");//灾害名称关键字
        String year = jsonObject.getString("zhaa01c020");//隐患点年份

        if (!StringUtil.IsNullOrEmpty(year))
            zhaa01c020 = Integer.parseInt(year);

//        List<Zhaa11aVO> hiddenDangerPointList =
//                riskManagementMapper.getHiddenDangerPointInfo(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);
//        List<Zhaa11aVO> hiddenCodeImageList =
//                riskManagementMapper.getHiddenCodeImageListo(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);

        List<Zhaa11aVO> hiddenDangerPointList =
                riskManagementMapper.getHiddenDangerPointInfo1(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);
        List<Zhaa11aVO> hiddenCodeImageList =
                riskManagementMapper.getHiddenCodeImageListo1(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);

        Map<String, Integer> countMap = new HashMap<>();
        Map<String, Integer> distributionMap = new HashMap<>();
        // 统计各个灾害类型的数量
        countMap = hiddenDangerPointList.stream()
                .collect(Collectors.groupingBy(Zhaa11aVO::getZhaa11A024, Collectors.summingInt(e -> 1)));

        // 计算总的灾害类型数量
        int totalCount = countMap.values().stream().mapToInt(Integer::intValue).sum();

        if (StringUtil.IsNullOrEmpty(zhaa11a032)){
            // 按县统计隐患点的数量
            distributionMap = hiddenDangerPointList.stream()
                    .collect(Collectors.groupingBy(Zhaa11aVO::getZhaa11A032,Collectors.summingInt(e -> 1)));
        }else {
            // 按乡统计隐患点的数量
            distributionMap = hiddenDangerPointList.stream()
                    .collect(Collectors.groupingBy(Zhaa11aVO::getZhaa11A034,Collectors.summingInt(e -> 1)));
        }


        // 将总计数量存入Map
        countMap.put("总计", totalCount);
        System.out.println("===================================================================================");
//        for (Zhaa11aVO zhaa11aVO : hiddenDangerPointList) {
//            System.out.println(zhaa11aVO);
//        }
//        for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
//            String disasterType = entry.getKey();
//            int count = entry.getValue();
//            System.out.println(disasterType + ":" + count);
//        }



        HiddenDangerVo hiddenDangerVo = new HiddenDangerVo(hiddenDangerPointList,hiddenCodeImageList, countMap,distributionMap);
        return hiddenDangerVo;
    }

    @Override
    public Object getHiddenDangerPointInfo2(JSONObject jsonObject) {
        if (JSONUtil.parseObj(jsonObject).isEmpty()) {
            return null;
        }
        Integer zhaa01c020 = null;
        String zhaa11a030 = jsonObject.getString("zhaa11a030");//市
        String zhaa11a032 = jsonObject.getString("zhaa11a032");//县
        String zhaa11a024 = jsonObject.getString("zhaa11a024");//灾害类型
        String zhaa11a012 = jsonObject.getString("zhaa11a012");//统一编号
        String zhaa11a022 = jsonObject.getString("zhaa11a022");//灾害名称关键字
//        String zhaa11a022 = jsonObject.getString("zhaa11a022");//灾害名称关键字
//        String zhaa11a022 = jsonObject.getString("zhaa11a022");//灾害名称关键字
//        String zhaa11a022 = jsonObject.getString("zhaa11a022");//灾害名称关键字
//        String zhaa11a022 = jsonObject.getString("zhaa11a022");//灾害名称关键字
//        String zhaa11a022 = jsonObject.getString("zhaa11a022");//灾害名称关键字
//        String zhaa11a022 = jsonObject.getString("zhaa11a022");//灾害名称关键字
        String year = jsonObject.getString("zhaa01c020");//隐患点年份

        if (!StringUtil.IsNullOrEmpty(year))
            zhaa01c020 = Integer.parseInt(year);

        List<Zhaa11aVO> hiddenDangerPointList =
                riskManagementMapper.getHiddenDangerPointInfo(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);
        List<Zhaa11aVO> hiddenCodeImageList =
                riskManagementMapper.getHiddenCodeImageListo(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);

//        List<Zhaa11aVO> hiddenDangerPointList =
//                riskManagementMapper.getHiddenDangerPointInfo1(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);
//        List<Zhaa11aVO> hiddenCodeImageList =
//                riskManagementMapper.getHiddenCodeImageListo1(zhaa11a030, zhaa11a032, zhaa11a024, zhaa11a012, zhaa11a022, zhaa01c020);

        Map<String, Integer> countMap = new HashMap<>();
        Map<String, Integer> distributionMap = new HashMap<>();
        // 统计各个灾害类型的数量
        countMap = hiddenDangerPointList.stream()
                .collect(Collectors.groupingBy(Zhaa11aVO::getZhaa11A024, Collectors.summingInt(e -> 1)));

        // 计算总的灾害类型数量
        int totalCount = countMap.values().stream().mapToInt(Integer::intValue).sum();

        if (StringUtil.IsNullOrEmpty(zhaa11a032)){
            // 按县统计隐患点的数量
            distributionMap = hiddenDangerPointList.stream()
                    .collect(Collectors.groupingBy(Zhaa11aVO::getZhaa11A032,Collectors.summingInt(e -> 1)));
        }else {
            // 按乡统计隐患点的数量
            distributionMap = hiddenDangerPointList.stream().map(i->{
                if (StringUtil.IsNullOrEmpty(i.getZhaa11A034())){
                    i.setZhaa11A034("其他");
                }
                return i;
            }).collect(Collectors.groupingBy(Zhaa11aVO::getZhaa11A034,Collectors.summingInt(e -> 1)));
        }


        // 将总计数量存入Map
        countMap.put("总计", totalCount);
        System.out.println("===================================================================================");
//        for (Zhaa11aVO zhaa11aVO : hiddenDangerPointList) {
//            System.out.println(zhaa11aVO);
//        }
//        for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
//            String disasterType = entry.getKey();
//            int count = entry.getValue();
//            System.out.println(disasterType + ":" + count);
//        }



        HiddenDangerVo hiddenDangerVo = new HiddenDangerVo(hiddenDangerPointList,hiddenCodeImageList, countMap,distributionMap);
        return hiddenDangerVo;
    }


}


